package cronjob

import (
	"context"
	"github.com/jinzhu/copier"
	"go-zero-admin/application/job/rpc/pb"

	"go-zero-admin/application/job/api/internal/svc"
	"go-zero-admin/application/job/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateCronLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 修改定时任务
func NewUpdateCronLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCronLogic {
	return &UpdateCronLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateCronLogic) UpdateCron(req *types.UpdateCronReq) (resp *types.UpdateCronRes, err error) {
	var grpcReq pb.UpdateCronInfo
	_ = copier.Copy(&grpcReq, req)
	res, err := l.svcCtx.JobCronSvc.UpdateCron(l.ctx, &pb.UpdateCronReq{UpdateCronInfo: &grpcReq})
	if err != nil {
		return nil, err
	}
	resp = &types.UpdateCronRes{}
	_ = copier.Copy(resp, res)

	return resp, nil
}
